package week5;

/**
 *
 * 875. 爱吃香蕉的珂珂
 * https://leetcode-cn.com/problems/koko-eating-bananas/
 *
 *
 * 执行结果：  通过  显示详情
 *
 * 添加备注
 * 执行用时：6 ms, 在所有 Java 提交中击败了99.86% 的用户
 * 内存消耗：39.8 MB, 在所有 Java 提交中击败了8.60% 的用户
 * 通过测试用例：118 / 118
 * 炫耀一下:
 *
 *
 */
public class Week5KokoEatingBananas {

    public boolean possible(int[] piles, int h, int k) {
        int time = 0;
        for(int pile : piles) {
            time += (pile - 1)/ k + 1;
        }
        return time <= h;
    }

    public int minEatingSpeed(int[] piles, int h) {
        int minSpeed = 1;
        int maxSpeed = 1_000_000_000;
        for(;minSpeed < maxSpeed;) {
            int middle = (minSpeed + maxSpeed) / 2;
            if(!possible(piles, h, middle)) {
                minSpeed = middle + 1;
            } else {
                maxSpeed = middle;
            }
        }

        return minSpeed;
    }
}
